package com.testing.redboy.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.connect.UserInfo;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.testing.redboy.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

public class QQFragment extends Fragment implements View.OnClickListener {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private ImageView qq_login;


    private Tencent mTencent;
    private String App_ID = "1105924500";
    private IUiListener loginListener;
    private IUiListener userInfoListener;
    private IUiListener shareInfoListener;
    //获取权限列表。所有权限为 all
    private static String SCOPE = "all";
    private ImageView touxiang;
    private TextView username;


    public QQFragment() {
    }

    public static QQFragment newInstance(String param1, String param2) {
        QQFragment fragment = new QQFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_qq, container, false);

        qq_login = (ImageView) v.findViewById(R.id.qq_login);
        touxiang = (ImageView) v.findViewById(R.id.touxiang);
        username = (TextView) v.findViewById(R.id.username);

        qq_login.setOnClickListener(this);

        initQqLogin();

        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.qq_login:
                mTencent.login(this, SCOPE, loginListener);
                break;
        }
    }

    private void initQqLogin() {

        try {
            mTencent = Tencent.createInstance(App_ID, getActivity());
        } catch (Exception e) {
            e.printStackTrace();
        }

        //创建QQ登录回调接口
        loginListener = new IUiListener() {
            @Override
            public void onComplete(Object o) {
                //登录成功后回调该方法
                Toast.makeText(getActivity(), "登录成功", Toast.LENGTH_SHORT).show();
                //设置openid，如果不设置这一步的话无法获取用户信息
                JSONObject jo = (JSONObject) o;
                String openID;
                try {
                    openID = jo.getString("openid");
                    String accessToken = jo.getString("access_token");
                    String expires = jo.getString("expires_in");
                    mTencent.setOpenId(openID);
                    mTencent.setAccessToken(accessToken, expires);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(UiError uiError) {
                //登录失败后回调该方法
                Toast.makeText(getActivity(), "登录失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel() {
                //取消登录后回调该方法
                Toast.makeText(getActivity(), "取消登录", Toast.LENGTH_SHORT).show();
            }
        };
        //获取用户信息的回调接口
        userInfoListener = new IUiListener() {
            @Override
            public void onComplete(Object o) {
                JSONObject jo = (JSONObject) o;
                try {
                    JSONObject info = (JSONObject) o;
                    //获取用户具体信息
                    String nickName = info.getString("nickname");
                    String figureurl_qq_2 = info.getString("figureurl_qq_2");


                    //为头像和昵称赋值
                    x.image().bind(touxiang, figureurl_qq_2);
                    username.setText(nickName);

                    qq_login.setVisibility(View.INVISIBLE);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(UiError uiError) {

            }

            @Override
            public void onCancel() {

            }
        };
    }

    //获取用户信息
    private void getUserInfo() {
        UserInfo info = new UserInfo(getActivity(), mTencent.getQQToken());
        info.getUserInfo(userInfoListener);
    }

    //QQ登陆回调
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        //判断登陆成功

        //QQ登陆回调
        // Tencent.onActivityResultData(requestCode, resultCode, data, loginListener);
        //一定要加上，不然监听不起作用
        Tencent.handleResultData(data, loginListener);
        //获取用户信息
        getUserInfo();


    }

}
